﻿Imports System.Data.SqlClient
Imports System.Drawing.Printing
Public Class Frm_a7kame
    Dim ds As New DataSet1
    Dim row As DataRow
    Public dre As SqlDataReader
    Dim t() As Integer
    Dim cmd As SqlClient.SqlCommand
    Dim y As Integer = 0
    Dim m As Integer = 1
    '----------------------------------------
    Dim tribunal As Boolean = False
    Dim numDossier As Boolean = False
    Dim mo9arir As Boolean = False
    Dim client As Boolean = False
    Dim adver As Boolean = False
    Dim avocAdvr As Boolean = False
    Dim datjalssa As Boolean = False
    Dim cause As Boolean = False
    Dim dicision As Boolean = False
    Dim datDicision As Boolean = False
    Public Sub Rech_Clients()
        Try

            Dim i As Integer = 0
            Dim idAdv As Integer = 0
            Dim str As String = ""
            Dim bol As Boolean = False 'ya imma a7kam ya imma ijra2aaat
            ds.Tables("a7kam").Rows.Clear()
            '------------------------------------
            Partager.deClt = DirectCast(Comb_nom.SelectedItem, DictionaryEntry)
            cmd = New SqlClient.SqlCommand("select idRef from refClt" & _
                                    " where idClt =" & CInt(Partager.deClt.Key), Partager.con)
            Partager.open()
            dre = cmd.ExecuteReader
            If dre.HasRows Then
                While dre.Read
                    ReDim Preserve t(i)
                    t(i) = dre("idRef")
                    i += 1
                End While
            Else
                dre.Close()
                Partager.close()
                Exit Sub
            End If
            dre.Close()
            Partager.close()
            For j As Integer = 0 To t.Count - 1
                For Each dos As Dossier In Partager.moi.List_info_fichier
                    If t(j) = dos.Ref Then
                        row = ds.Tables("a7kam").NewRow()
                        row("الموكـل") = Comb_nom.Text
                        '----------------------------
                        row("المحكمـة") = dos.Tribunal
                        row("رقم الملف") = dos.Num
                        '-----------------------------------------
                        dos.remplir_List_mo9arir()
                        For Each mo9 As M9arir In dos.List_mo9arir
                            If mo9.actif = True Then
                                row("المقــرر") = mo9.Nom
                                Exit For
                            End If
                        Next
                        '-----------------------------------------
                        dos.remplir_List_decisions()
                        bol = False
                        For Each hok As decisions In dos.List_decisions
                            If hok.Type = "قطعي" Then
                                row("الحكم أو القرار") = hok.Declamation
                                row("تاريخ الحكم أو القرار") = Format(hok.Date_decision, "dd/MM/yyyy")
                                bol = True
                                Exit For
                            End If
                        Next
                        '-----------------------------------------
                        If bol = False Then
                            dos.remplir_List_ijraa()
                            Dim r = From ijr As ijraa In dos.List_ijraa
                                    Where ijr.Date_ijraa = (Aggregate maxe As ijraa In dos.List_ijraa Into Max(maxe.Date_ijraa))
                                    Select ijr.Date_ijraa, ijr.Cause

                            For Each ijra In r
                                row("تاريخ آخر جلسة") = Format(ijra.Date_ijraa, "dd/MM/yyyy")
                                row("السبب") = ijra.Cause
                                Exit For
                            Next
                        End If
                        '--------------------------------------------------------------
                        idAdv = 0
                        str = ""
                        cmd = New SqlClient.SqlCommand("select idAdv from refAdv" & _
                                   " where idRef =" & dos.Ref, Partager.con)
                        Partager.open()
                        dre = cmd.ExecuteReader
                        If dre.HasRows Then
                            dre.Read()
                            idAdv = dre("idAdv")
                        End If
                        dre.Close()
                        Partager.close()

                        For Each enm As Enemie In Partager.moi.List_enemi
                            If idAdv = enm.Id Then
                                If enm.Type = "ذاتي" Then
                                    str = enm.Nom & "  " & enm.Prenom
                                ElseIf enm.Type = "معنوي" Then
                                    str = enm.Nom_ent
                                End If
                                Exit For
                            End If
                        Next
                        row("الخصـم") = str
                        '----------------------------------------------------
                        str = ""
                        dos.remplir_List_avocat_enemie()
                        For Each dsAvAd As Dossier_Avocat_Adversaire In dos.List_avocat_enemie
                            If dsAvAd.nomme = True Then
                                For Each av As Avocat_enemie In Partager.moi.List_avocat_enemie
                                    If dsAvAd.Id_Avocat_Adversaire = av.Id Then
                                        str = av.Nom_a & "  " & av.Prenom_a
                                        Exit For
                                    End If
                                Next
                                Exit For
                            End If
                        Next
                        row("محامي الخصم") = str
                        '----------------------------------------------------
                        If Comb_ahkamIjra2.Text = "" Then
                            ds.Tables("a7kam").Rows.Add(row)
                        ElseIf Comb_ahkamIjra2.Text = "الأحكام و القرارات" And Not IsDBNull(row("تاريخ الحكم أو القرار")) Then
                            If CDate(row("تاريخ الحكم أو القرار")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ الحكم أو القرار")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                ds.Tables("a7kam").Rows.Add(row)
                            End If
                        ElseIf Comb_ahkamIjra2.Text = "الإجراءات" And Not IsDBNull(row("تاريخ آخر جلسة")) Then
                            If CDate(row("تاريخ آخر جلسة")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ آخر جلسة")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                ds.Tables("a7kam").Rows.Add(row)
                            End If
                        ElseIf Comb_ahkamIjra2.Text = "الأحكام و القرارات و الإجراءات" Then

                            If Not IsDBNull(row("تاريخ الحكم أو القرار")) Then
                                If CDate(row("تاريخ الحكم أو القرار")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ الحكم أو القرار")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                    ds.Tables("a7kam").Rows.Add(row)
                                End If
                            End If
                            '---------------------------------
                            If Not IsDBNull(row("تاريخ آخر جلسة")) Then
                                If CDate(row("تاريخ آخر جلسة")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ آخر جلسة")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                    ds.Tables("a7kam").Rows.Add(row)
                                End If
                            End If
                        End If

                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    '-----------------------------------------------------------------
    Public Sub Rech_Advr()
        Try
            Dim i As Integer = 0
            Dim comb As ComboBox = Comb_nom
            Dim idclt As Integer = 0
            Dim str As String = ""
            Dim bol As Boolean = False 'ya imma a7kam ya imma ijra2aaat
            ds.Tables("a7kam").Rows.Clear()
            '------------------------------------
            comb.Name = "Comb_adv"
            Partager.deClt = DirectCast(comb.SelectedItem, DictionaryEntry)
            cmd = New SqlClient.SqlCommand("select idRef from refAdv" & _
                                    " where idAdv =" & CInt(Partager.deClt.Key), Partager.con)
            Partager.open()
            dre = cmd.ExecuteReader
            If dre.HasRows Then
                While dre.Read
                    ReDim Preserve t(i)
                    t(i) = dre("idRef")
                    i += 1
                End While
            Else
                dre.Close()
                Partager.close()
                Exit Sub
            End If
            dre.Close()
            Partager.close()
            For j As Integer = 0 To t.Count - 1
                For Each dos As Dossier In Partager.moi.List_info_fichier
                    If t(j) = dos.Ref Then
                        row = ds.Tables("a7kam").NewRow()
                        row("الخصـم") = comb.Text
                        '----------------------------
                        row("المحكمـة") = dos.Tribunal
                        row("رقم الملف") = dos.Num
                        '-----------------------------------------
                        dos.remplir_List_mo9arir()
                        For Each mo9 As M9arir In dos.List_mo9arir
                            If mo9.actif = True Then
                                row("المقــرر") = mo9.Nom
                                Exit For
                            End If
                        Next
                        '-----------------------------------------
                        dos.remplir_List_decisions()
                        bol = False
                        For Each hok As decisions In dos.List_decisions
                            If hok.Type = "قطعي" Then
                                row("الحكم أو القرار") = hok.Declamation
                                row("تاريخ الحكم أو القرار") = Format(hok.Date_decision, "dd/MM/yyyy")
                                bol = True
                                Exit For
                            End If
                        Next
                        '-----------------------------------------
                        If bol = False Then
                            dos.remplir_List_ijraa()
                            Dim r = From ijr As ijraa In dos.List_ijraa
                                    Where ijr.Date_ijraa = (Aggregate maxe As ijraa In dos.List_ijraa Into Max(maxe.Date_ijraa))
                                    Select ijr.Date_ijraa, ijr.Cause

                            For Each ijra In r
                                row("تاريخ آخر جلسة") = Format(ijra.Date_ijraa, "dd/MM/yyyy")
                                row("السبب") = ijra.Cause
                                Exit For
                            Next
                        End If
                        '--------------------------------------------------------------
                        idclt = 0
                        str = ""
                        cmd = New SqlClient.SqlCommand("select idClt from refClt" & _
                                   " where idRef =" & dos.Ref, Partager.con)
                        Partager.open()
                        dre = cmd.ExecuteReader
                        If dre.HasRows Then
                            dre.Read()
                            idclt = dre("idClt")
                        End If
                        dre.Close()
                        Partager.close()

                        For Each clt As Client In Partager.moi.List_client
                            If idclt = clt.Id Then
                                If clt.Type = "ذاتي" Then
                                    str = clt.Nom & "  " & clt.Prenom
                                ElseIf clt.Type = "معنوي" Then
                                    str = clt.Nom_ent
                                End If
                                Exit For
                            End If
                        Next
                        row("الموكـل") = str
                        '----------------------------------------------------
                        str = ""
                        dos.remplir_List_avocat_enemie()
                        For Each dsAvAd As Dossier_Avocat_Adversaire In dos.List_avocat_enemie
                            If dsAvAd.nomme = True Then
                                For Each av As Avocat_enemie In Partager.moi.List_avocat_enemie
                                    If dsAvAd.Id_Avocat_Adversaire = av.Id Then
                                        str = av.Nom_a & "  " & av.Prenom_a
                                        Exit For
                                    End If
                                Next
                                Exit For
                            End If
                        Next
                        row("محامي الخصم") = str
                        '----------------------------------------------------
                        If Comb_ahkamIjra2.Text = "" Then
                            ds.Tables("a7kam").Rows.Add(row)
                        ElseIf Comb_ahkamIjra2.Text = "الأحكام و القرارات" And Not IsDBNull(row("تاريخ الحكم أو القرار")) Then
                            If CDate(row("تاريخ الحكم أو القرار")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ الحكم أو القرار")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                ds.Tables("a7kam").Rows.Add(row)
                            End If
                        ElseIf Comb_ahkamIjra2.Text = "الإجراءات" And Not IsDBNull(row("تاريخ آخر جلسة")) Then
                            If CDate(row("تاريخ آخر جلسة")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ آخر جلسة")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                ds.Tables("a7kam").Rows.Add(row)
                            End If
                        ElseIf Comb_ahkamIjra2.Text = "الأحكام و القرارات و الإجراءات" Then

                            If Not IsDBNull(row("تاريخ الحكم أو القرار")) Then
                                If CDate(row("تاريخ الحكم أو القرار")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ الحكم أو القرار")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                    ds.Tables("a7kam").Rows.Add(row)
                                End If
                            End If
                            '---------------------------------
                            If Not IsDBNull(row("تاريخ آخر جلسة")) Then
                                If CDate(row("تاريخ آخر جلسة")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ آخر جلسة")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                    ds.Tables("a7kam").Rows.Add(row)
                                End If
                            End If
                        End If
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    '------------------------------------------------------------------------------------
    Public Sub Rech_momatilElkanouni(ByVal req As String)
        Try
            Dim i As Integer = 0
            Dim idclt As Integer = 0
            Dim idAdv As Integer = 0
            Dim str As String = ""
            Dim bol As Boolean = False 'ya imma a7kam ya imma ijra2aaat
            ds.Tables("a7kam").Rows.Clear()
            '------------------------------------
            cmd = New SqlClient.SqlCommand(req & Comb_nom.SelectedItem & "'", Partager.con)
            Partager.open()
            dre = cmd.ExecuteReader
            If dre.HasRows Then
                While dre.Read
                    ReDim Preserve t(i)
                    t(i) = dre("idRef")
                    i += 1
                End While
            Else
                dre.Close()
                Partager.close()
                Exit Sub
            End If
            dre.Close()
            Partager.close()
            For j As Integer = 0 To t.Count - 1
                For Each dos As Dossier In Partager.moi.List_info_fichier
                    If t(j) = dos.Ref Then
                        row = ds.Tables("a7kam").NewRow()
                        '----------------------------
                        row("المحكمـة") = dos.Tribunal
                        row("رقم الملف") = dos.Num
                        '-----------------------------------------
                        dos.remplir_List_mo9arir()
                        For Each mo9 As M9arir In dos.List_mo9arir
                            If mo9.actif = True Then
                                row("المقــرر") = mo9.Nom
                                Exit For
                            End If
                        Next
                        '-----------------------------------------
                        dos.remplir_List_decisions()
                        bol = False
                        For Each hok As decisions In dos.List_decisions
                            If hok.Type = "قطعي" Then
                                row("الحكم أو القرار") = hok.Declamation
                                row("تاريخ الحكم أو القرار") = Format(hok.Date_decision, "dd/MM/yyyy")
                                bol = True
                                Exit For
                            End If
                        Next
                        '-----------------------------------------
                        If bol = False Then
                            dos.remplir_List_ijraa()
                            Dim r = From ijr As ijraa In dos.List_ijraa
                                    Where ijr.Date_ijraa = (Aggregate maxe As ijraa In dos.List_ijraa Into Max(maxe.Date_ijraa))
                                    Select ijr.Date_ijraa, ijr.Cause

                            For Each ijra In r
                                row("تاريخ آخر جلسة") = Format(ijra.Date_ijraa, "dd/MM/yyyy")
                                row("السبب") = ijra.Cause
                                Exit For
                            Next
                        End If
                        '--------------------------------------------------------------
                        idAdv = 0
                        str = ""
                        cmd = New SqlClient.SqlCommand("select idAdv from refAdv" & _
                                   " where idRef =" & dos.Ref, Partager.con)
                        Partager.open()
                        dre = cmd.ExecuteReader
                        If dre.HasRows Then
                            dre.Read()
                            idAdv = dre("idAdv")
                        End If
                        dre.Close()
                        Partager.close()

                        For Each enm As Enemie In Partager.moi.List_enemi
                            If idAdv = enm.Id Then
                                If enm.Type = "ذاتي" Then
                                    str = enm.Nom & "  " & enm.Prenom
                                ElseIf enm.Type = "معنوي" Then
                                    str = enm.Nom_ent
                                End If
                                Exit For
                            End If
                        Next
                        row("الخصـم") = str
                        '--------------------------------------------------------------
                        idclt = 0
                        str = ""
                        cmd = New SqlClient.SqlCommand("select idClt from refClt" & _
                                   " where idRef =" & dos.Ref, Partager.con)
                        Partager.open()
                        dre = cmd.ExecuteReader
                        If dre.HasRows Then
                            dre.Read()
                            idclt = dre("idClt")
                        End If
                        dre.Close()
                        Partager.close()

                        For Each clt As Client In Partager.moi.List_client
                            If idclt = clt.Id Then
                                If clt.Type = "ذاتي" Then
                                    str = clt.Nom & "  " & clt.Prenom
                                ElseIf clt.Type = "معنوي" Then
                                    str = clt.Nom_ent
                                End If
                                Exit For
                            End If
                        Next
                        row("الموكـل") = str
                        '----------------------------------------------------
                        str = ""
                        dos.remplir_List_avocat_enemie()
                        For Each dsAvAd As Dossier_Avocat_Adversaire In dos.List_avocat_enemie
                            If dsAvAd.nomme = True Then
                                For Each av As Avocat_enemie In Partager.moi.List_avocat_enemie
                                    If dsAvAd.Id_Avocat_Adversaire = av.Id Then
                                        str = av.Nom_a & "  " & av.Prenom_a
                                        Exit For
                                    End If
                                Next
                                Exit For
                            End If
                        Next
                        row("محامي الخصم") = str
                        '----------------------------------------------------
                        If Comb_ahkamIjra2.Text = "" Then
                            ds.Tables("a7kam").Rows.Add(row)
                        ElseIf Comb_ahkamIjra2.Text = "الأحكام و القرارات" And Not IsDBNull(row("تاريخ الحكم أو القرار")) Then
                            If CDate(row("تاريخ الحكم أو القرار")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ الحكم أو القرار")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                ds.Tables("a7kam").Rows.Add(row)
                            End If
                        ElseIf Comb_ahkamIjra2.Text = "الإجراءات" And Not IsDBNull(row("تاريخ آخر جلسة")) Then
                            If CDate(row("تاريخ آخر جلسة")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ آخر جلسة")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                ds.Tables("a7kam").Rows.Add(row)
                            End If
                        ElseIf Comb_ahkamIjra2.Text = "الأحكام و القرارات و الإجراءات" Then

                            If Not IsDBNull(row("تاريخ الحكم أو القرار")) Then
                                If CDate(row("تاريخ الحكم أو القرار")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ الحكم أو القرار")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                    ds.Tables("a7kam").Rows.Add(row)
                                End If
                            End If
                            '---------------------------------
                            If Not IsDBNull(row("تاريخ آخر جلسة")) Then
                                If CDate(row("تاريخ آخر جلسة")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ آخر جلسة")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                    ds.Tables("a7kam").Rows.Add(row)
                                End If
                            End If
                        End If
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    '---------------------------------------------------------------------------------------
    Public Sub Rech_Avo_Adver(ByVal req As String, ByVal type As String)
        Try
            Dim i As Integer = 0
            Dim idclt As Integer = 0
            Dim idAdv As Integer = 0
            Dim str As String = ""
            Dim bol As Boolean = False 'ya imma a7kam ya imma ijra2aaat
            ds.Tables("a7kam").Rows.Clear()
            '------------------------------------
            Partager.deClt = DirectCast(Comb_nom.SelectedItem, DictionaryEntry)
            cmd = New SqlClient.SqlCommand(req & CInt(Partager.deClt.Key), Partager.con)
            Partager.open()
            dre = cmd.ExecuteReader
            If dre.HasRows Then
                While dre.Read
                    ReDim Preserve t(i)
                    t(i) = dre("Ref")
                    i += 1
                End While
            Else
                dre.Close()
                Partager.close()
                Exit Sub
            End If
            dre.Close()
            Partager.close()
            For j As Integer = 0 To t.Count - 1
                For Each dos As Dossier In Partager.moi.List_info_fichier
                    If t(j) = dos.Ref Then
                        row = ds.Tables("a7kam").NewRow()
                        If type = "advr" Then
                            row("محامي الخصم") = Comb_nom.Text
                        End If
                        '----------------------------
                        row("المحكمـة") = dos.Tribunal
                        row("رقم الملف") = dos.Num
                        '-----------------------------------------
                        dos.remplir_List_mo9arir()
                        For Each mo9 As M9arir In dos.List_mo9arir
                            If mo9.actif = True Then
                                row("المقــرر") = mo9.Nom
                                Exit For
                            End If
                        Next
                        '-----------------------------------------
                        dos.remplir_List_decisions()
                        bol = False
                        For Each hok As decisions In dos.List_decisions
                            If hok.Type = "قطعي" Then
                                row("الحكم أو القرار") = hok.Declamation
                                row("تاريخ الحكم أو القرار") = Format(hok.Date_decision, "dd/MM/yyyy")
                                bol = True
                                Exit For
                            End If
                        Next
                        '-----------------------------------------
                        If bol = False Then
                            dos.remplir_List_ijraa()
                            Dim r = From ijr As ijraa In dos.List_ijraa
                                    Where ijr.Date_ijraa = (Aggregate maxe As ijraa In dos.List_ijraa Into Max(maxe.Date_ijraa))
                                    Select ijr.Date_ijraa, ijr.Cause

                            For Each ijra In r
                                row("تاريخ آخر جلسة") = Format(ijra.Date_ijraa, "dd/MM/yyyy")
                                row("السبب") = ijra.Cause
                                Exit For
                            Next
                        End If
                        '--------------------------------------------------------------
                        idclt = 0
                        str = ""
                        cmd = New SqlClient.SqlCommand("select idClt from refClt" & _
                                   " where idRef =" & dos.Ref, Partager.con)
                        Partager.open()
                        dre = cmd.ExecuteReader
                        If dre.HasRows Then
                            dre.Read()
                            idclt = dre("idClt")
                        End If
                        dre.Close()
                        Partager.close()

                        For Each clt As Client In Partager.moi.List_client
                            If idclt = clt.Id Then
                                If clt.Type = "ذاتي" Then
                                    str = clt.Nom & "  " & clt.Prenom
                                ElseIf clt.Type = "معنوي" Then
                                    str = clt.Nom_ent
                                End If
                                Exit For
                            End If
                        Next
                        row("الموكـل") = str
                        '----------------------------------------------------
                        idAdv = 0
                        str = ""
                        cmd = New SqlClient.SqlCommand("select idAdv from refAdv" & _
                                   " where idRef =" & dos.Ref, Partager.con)
                        Partager.open()
                        dre = cmd.ExecuteReader
                        If dre.HasRows Then
                            dre.Read()
                            idAdv = dre("idAdv")
                        End If
                        dre.Close()
                        Partager.close()

                        For Each enm As Enemie In Partager.moi.List_enemi
                            If idAdv = enm.Id Then
                                If enm.Type = "ذاتي" Then
                                    str = enm.Nom & "  " & enm.Prenom
                                ElseIf enm.Type = "معنوي" Then
                                    str = enm.Nom_ent
                                End If
                                Exit For
                            End If
                        Next
                        row("الخصـم") = str
                        '----------------------------------------------------
                        If type = "clt" Then
                            str = ""
                            dos.remplir_List_avocat_enemie()
                            For Each dsAvAd As Dossier_Avocat_Adversaire In dos.List_avocat_enemie
                                If dsAvAd.nomme = True Then
                                    For Each av As Avocat_enemie In Partager.moi.List_avocat_enemie
                                        If dsAvAd.Id_Avocat_Adversaire = av.Id Then
                                            str = av.Nom_a & "  " & av.Prenom_a
                                            Exit For
                                        End If
                                    Next
                                    Exit For
                                End If
                            Next
                            row("محامي الخصم") = str
                        End If
                        '----------------------------------------------------
                        If Comb_ahkamIjra2.Text = "" Then
                            ds.Tables("a7kam").Rows.Add(row)
                        ElseIf Comb_ahkamIjra2.Text = "الأحكام و القرارات" And Not IsDBNull(row("تاريخ الحكم أو القرار")) Then
                            If CDate(row("تاريخ الحكم أو القرار")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ الحكم أو القرار")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                ds.Tables("a7kam").Rows.Add(row)
                            End If
                        ElseIf Comb_ahkamIjra2.Text = "الإجراءات" And Not IsDBNull(row("تاريخ آخر جلسة")) Then
                            If CDate(row("تاريخ آخر جلسة")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ آخر جلسة")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                ds.Tables("a7kam").Rows.Add(row)
                            End If
                        ElseIf Comb_ahkamIjra2.Text = "الأحكام و القرارات و الإجراءات" Then

                            If Not IsDBNull(row("تاريخ الحكم أو القرار")) Then
                                If CDate(row("تاريخ الحكم أو القرار")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ الحكم أو القرار")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                    ds.Tables("a7kam").Rows.Add(row)
                                End If
                            End If
                            '---------------------------------
                            If Not IsDBNull(row("تاريخ آخر جلسة")) Then
                                If CDate(row("تاريخ آخر جلسة")) >= Format(Date_min.Value.Date, "dd/MM/yyyy") And CDate(row("تاريخ آخر جلسة")) <= Format(Date_illa.Value.Date, "dd/MM/yyyy") Then
                                    ds.Tables("a7kam").Rows.Add(row)
                                End If
                            End If
                        End If
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    '-----------------------------------------------------------------------------------------------------------------
    Public Sub Rech_ahkam(ByVal req As String)
        Try
            Dim i As Integer = 0
            Dim idclt As Integer = 0
            Dim idAdv As Integer = 0
            Dim str As String = ""
            Dim bol As Boolean = False 'ya imma a7kam ya imma ijra2aaat
            ds.Tables("a7kam").Rows.Clear()
            '------------------------------------
            cmd = New SqlClient.SqlCommand(req, Partager.con)
            Partager.open()
            dre = cmd.ExecuteReader
            If dre.HasRows Then
                While dre.Read
                    ReDim Preserve t(i)
                    t(i) = dre("id")
                    i += 1
                End While
            Else
                dre.Close()
                Partager.close()
                Exit Sub
            End If
            dre.Close()
            Partager.close()
            For j As Integer = 0 To t.Count - 1
                For Each dos As Dossier In Partager.moi.List_info_fichier
                    If t(j) = dos.id Then
                        row = ds.Tables("a7kam").NewRow()
                        '----------------------------
                        row("المحكمـة") = dos.Tribunal
                        row("رقم الملف") = dos.Num
                        '-----------------------------------------
                        dos.remplir_List_mo9arir()
                        For Each mo9 As M9arir In dos.List_mo9arir
                            If mo9.actif = True Then
                                row("المقــرر") = mo9.Nom
                                Exit For
                            End If
                        Next
                        '-----------------------------------------
                        bol = False
                        If Comb_ahkamIjra2.Text <> "الإجراءات" Then
                            dos.remplir_List_decisions()
                            bol = False
                            For Each hok As decisions In dos.List_decisions
                                If hok.Type = "قطعي" Then
                                    row("الحكم أو القرار") = hok.Declamation
                                    row("تاريخ الحكم أو القرار") = Format(hok.Date_decision, "dd/MM/yyyy")
                                    bol = True
                                    Exit For
                                End If
                            Next
                        End If
                        '-----------------------------------------
                        If Comb_ahkamIjra2.Text <> "الأحكام و القرارات" Then
                            If bol = False Then
                                dos.remplir_List_ijraa()
                                Dim r = From ijr As ijraa In dos.List_ijraa
                                        Where ijr.Date_ijraa = (Aggregate maxe As ijraa In dos.List_ijraa Into Max(maxe.Date_ijraa))
                                        Select ijr.Date_ijraa, ijr.Cause

                                For Each ijra In r
                                    row("تاريخ آخر جلسة") = Format(ijra.Date_ijraa, "dd/MM/yyyy")
                                    row("السبب") = ijra.Cause
                                    Exit For
                                Next
                            End If
                        End If
                        '--------------------------------------------------------------
                        idAdv = 0
                        str = ""
                        cmd = New SqlClient.SqlCommand("select idAdv from refAdv" & _
                                   " where idRef =" & dos.Ref, Partager.con)
                        Partager.open()
                        dre = cmd.ExecuteReader
                        If dre.HasRows Then
                            dre.Read()
                            idAdv = dre("idAdv")
                        End If
                        dre.Close()
                        Partager.close()

                        For Each enm As Enemie In Partager.moi.List_enemi
                            If idAdv = enm.Id Then
                                If enm.Type = "ذاتي" Then
                                    str = enm.Nom & "  " & enm.Prenom
                                ElseIf enm.Type = "معنوي" Then
                                    str = enm.Nom_ent
                                End If
                                Exit For
                            End If
                        Next
                        row("الخصـم") = str
                        '--------------------------------------------------------------
                        idclt = 0
                        str = ""
                        cmd = New SqlClient.SqlCommand("select idClt from refClt" & _
                                   " where idRef =" & dos.Ref, Partager.con)
                        Partager.open()
                        dre = cmd.ExecuteReader
                        If dre.HasRows Then
                            dre.Read()
                            idclt = dre("idClt")
                        End If
                        dre.Close()
                        Partager.close()

                        For Each clt As Client In Partager.moi.List_client
                            If idclt = clt.Id Then
                                If clt.Type = "ذاتي" Then
                                    str = clt.Nom & "  " & clt.Prenom
                                ElseIf clt.Type = "معنوي" Then
                                    str = clt.Nom_ent
                                End If
                                Exit For
                            End If
                        Next
                        row("الموكـل") = str
                        '----------------------------------------------------
                        str = ""
                        dos.remplir_List_avocat_enemie()
                        For Each dsAvAd As Dossier_Avocat_Adversaire In dos.List_avocat_enemie
                            If dsAvAd.nomme = True Then
                                For Each av As Avocat_enemie In Partager.moi.List_avocat_enemie
                                    If dsAvAd.Id_Avocat_Adversaire = av.Id Then
                                        str = av.Nom_a & "  " & av.Prenom_a
                                        Exit For
                                    End If
                                Next
                                Exit For
                            End If
                        Next
                        row("محامي الخصم") = str
                        '----------------------------------------------------
                        ds.Tables("a7kam").Rows.Add(row)
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub columns()
        tribunal = False
        numDossier = False
        mo9arir = False
        client = False
        adver = False
        avocAdvr = False
        datjalssa = False
        cause = False
        dicision = False
        datDicision = False

        If IsNothing(Partager.tabColumns) Then
            tribunal = False
            numDossier = False
            mo9arir = False
            client = False
            adver = False
            avocAdvr = False
            datjalssa = False
            cause = False
            dicision = False
            datDicision = False
        Else
            For i As Integer = 0 To Partager.tabColumns.Count - 1
                If Partager.tabColumns(i) = "المحكمة" Then
                    tribunal = True
                End If
                If Partager.tabColumns(i) = "رقم الملف" Then
                    numDossier = True
                End If
                If Partager.tabColumns(i) = "المقــرر" Then
                    mo9arir = True
                End If
                If Partager.tabColumns(i) = "الموكـل" Then
                    client = True
                End If
                If Partager.tabColumns(i) = "الخصـم" Then
                    adver = True
                End If
                If Partager.tabColumns(i) = "محامي الخصم" Then
                    avocAdvr = True
                End If
                If Partager.tabColumns(i) = "تاريخ آخر جلسة" Then

                    datjalssa = True
                End If
                If Partager.tabColumns(i) = "السبب" Then
                    cause = True
                End If
                If Partager.tabColumns(i) = "الحكم أو القرار" Then
                    dicision = True
                End If
                If Partager.tabColumns(i) = "تاريخ الحكم أو القرار" Then
                    datDicision = True
                End If
            Next
        End If
       
    End Sub
    Public Sub remplir()
        Try
            columns()
            DataGridView1.Rows.Clear()
            DataGridView1.Columns.Clear()
            If IsNothing(Partager.tabColumns) Then
                DataGridView1.ColumnCount = 0
            Else
                DataGridView1.ColumnCount = Partager.tabColumns.Count
            End If
            If Not IsNothing(Partager.tabColumns) Then

                Dim j As Integer = 0
                For i As Integer = 0 To ds.Tables("a7kam").Rows.Count - 1
                    j = 0
                    Dim rows As New DataGridViewRow
                    DataGridView1.Rows.Add(rows)
                    If tribunal Then
                        If i = 0 Then
                            DataGridView1.Columns(j).Name = "المحكمـة"
                        End If
                        If ds.Tables("a7kam").Rows(i).Item(0) IsNot DBNull.Value Then
                            DataGridView1.Rows(i).Cells(j).Value = ds.Tables("a7kam").Rows(i).Item(0)
                        Else
                            DataGridView1.Rows(i).Cells(j).Value = ""
                        End If
                        j += 1
                    End If
                    If numDossier Then
                        If i = 0 Then
                            DataGridView1.Columns(j).Name = "رقم الملف"
                        End If
                        If ds.Tables("a7kam").Rows(i).Item(6) IsNot DBNull.Value Then
                            DataGridView1.Rows(i).Cells(j).Value = ds.Tables("a7kam").Rows(i).Item(1)
                        Else
                            DataGridView1.Rows(i).Cells(j).Value = ""
                        End If
                        j += 1
                    End If
                    If mo9arir Then
                        If i = 0 Then
                            DataGridView1.Columns(j).Name = "المقــرر"
                        End If
                        If ds.Tables("a7kam").Rows(i).Item(2) IsNot DBNull.Value Then
                            DataGridView1.Rows(i).Cells(j).Value = ds.Tables("a7kam").Rows(i).Item(2)
                        Else
                            DataGridView1.Rows(i).Cells(j).Value = ""
                        End If
                        j += 1
                    End If
                    If client Then
                        If i = 0 Then
                            DataGridView1.Columns(j).Name = "الموكـل"
                        End If
                        If ds.Tables("a7kam").Rows(i).Item(3) IsNot DBNull.Value Then
                            DataGridView1.Rows(i).Cells(j).Value = ds.Tables("a7kam").Rows(i).Item(3)
                        Else
                            DataGridView1.Rows(i).Cells(j).Value = ""
                        End If
                        j += 1
                    End If
                    If adver Then
                        If i = 0 Then
                            DataGridView1.Columns(j).Name = "الخصـم"
                        End If
                        If ds.Tables("a7kam").Rows(i).Item(4) IsNot DBNull.Value Then
                            DataGridView1.Rows(i).Cells(j).Value = ds.Tables("a7kam").Rows(i).Item(4)
                        Else
                            DataGridView1.Rows(i).Cells(j).Value = ""
                        End If
                        j += 1
                    End If
                    If avocAdvr Then
                        If i = 0 Then
                            DataGridView1.Columns(j).Name = "محامي الخصم"
                        End If
                        If ds.Tables("a7kam").Rows(i).Item(5) IsNot DBNull.Value Then
                            DataGridView1.Rows(i).Cells(j).Value = ds.Tables("a7kam").Rows(i).Item(5)
                        Else
                            DataGridView1.Rows(i).Cells(j).Value = ""
                        End If
                        j += 1
                    End If
                    If datjalssa Then
                        If i = 0 Then
                            DataGridView1.Columns(j).Name = "تاريخ آخر جلسة"
                            DataGridView1.Columns(j).ValueType = GetType(DateTime)
                        End If
                        If ds.Tables("a7kam").Rows(i).Item(6) IsNot DBNull.Value Then
                            DataGridView1.Rows(i).Cells(j).Value = ds.Tables("a7kam").Rows(i).Item(6)
                        Else
                            DataGridView1.Rows(i).Cells(j).Value = ""
                        End If
                        j += 1
                    End If
                    If cause Then
                        If i = 0 Then
                            DataGridView1.Columns(j).Name = "السبب"
                        End If
                        If ds.Tables("a7kam").Rows(i).Item(7) IsNot DBNull.Value Then
                            DataGridView1.Rows(i).Cells(j).Value = ds.Tables("a7kam").Rows(i).Item(7)
                        Else
                            DataGridView1.Rows(i).Cells(j).Value = ""
                        End If
                        j += 1
                    End If
                    If dicision Then
                        If i = 0 Then
                            DataGridView1.Columns(j).Name = "الحكم أو القرار"
                        End If
                        If ds.Tables("a7kam").Rows(i).Item(8) IsNot DBNull.Value Then
                            DataGridView1.Rows(i).Cells(j).Value = ds.Tables("a7kam").Rows(i).Item(8)
                        Else
                            DataGridView1.Rows(i).Cells(j).Value = ""
                        End If
                        j += 1
                    End If
                    If datDicision Then
                        If i = 0 Then
                            DataGridView1.Columns(j).Name = "تاريخ الحكم أو القرار"
                            DataGridView1.Columns(j).ValueType = GetType(DateTime)
                        End If
                        If ds.Tables("a7kam").Rows(i).Item(9) IsNot DBNull.Value Then
                            DataGridView1.Rows(i).Cells(j).Value = ds.Tables("a7kam").Rows(i).Item(9)
                        Else
                            DataGridView1.Rows(i).Cells(j).Value = ""
                        End If
                        j += 1
                    End If
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Frm_a7kame_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Partager.tabColumns = Nothing
    End Sub

    Private Sub Frm_a7kame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        Partager.moi.remplir_List_info_fichier()
        Partager.moi.remplir_List_enemi()
        Partager.moi.remplir_List_avocat_enemie()
        Partager.moi.remplir_List_client()
        Comb_ahkamIjra2.SelectedIndex = 0
        '----------------------------------------------------------
        'Remplir le tableau des columns
        Dim i As Integer = 0
        For Each check As CheckBox In Frm_Columns.Controls
            ReDim Preserve Partager.tabColumns(i)
            Partager.tabColumns(i) = check.Text
            i += 1
        Next
    End Sub

    Private Sub Comb_choisi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_choisi.SelectedIndexChanged
        If Comb_choisi.Text = "الموكل" Then
            Partager.remplirClient(Comb_nom)
        ElseIf Comb_choisi.Text = "الخصم" Then
            Partager.remplirEnemie(Comb_nom)
        ElseIf Comb_choisi.Text = "الممثل القانوني للموكل" Then
            Partager.remplirmomatil(Comb_nom, "clients")
        ElseIf Comb_choisi.Text = "الممثل القانوني للخصم" Then
            Partager.remplirmomatil(Comb_nom, "Adversaire")
        ElseIf Comb_choisi.Text = "محامي الخصم" Then
            Comb_nom.Text = ""
            Comb_nom.Items.Clear()
            Frm_Av_Enemi.remplirMo7ami(Comb_nom)
        ElseIf Comb_choisi.Text = "محامي المكتب" Then
            Comb_nom.Text = ""
            Comb_nom.Items.Clear()
            Frm_Av_Mowakil.remplirMo7ami(Comb_nom)
        End If
    End Sub

    Private Sub Btn_Rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech.Click
        If Comb_choisi.Text = "" And Comb_ahkamIjra2.Text = "" Then
            MsgBox("عفوا، عليك اختيار ما تريد البحث عليه", MsgBoxStyle.Information, "خطأ")
        Else
            If Comb_choisi.Text = "الموكل" Then
                Rech_Clients()
            ElseIf Comb_choisi.Text = "الخصم" Then
                Rech_Advr()
            ElseIf Comb_choisi.Text = "الممثل القانوني للموكل" Then
                Rech_momatilElkanouni("select distinct idRef from refClt,Clients where Clients.id = refClt.idClt and Representant_legal='")
            ElseIf Comb_choisi.Text = "الممثل القانوني للخصم" Then
                Rech_momatilElkanouni("select distinct idRef from refAdv,Adversaire where Adversaire.id = refAdv.idAdv and Representant_legal='")
            ElseIf Comb_choisi.Text = "محامي الخصم" Then
                Rech_Avo_Adver("select Ref from Dossier,Dossier_Avocat_Adversaire where Dossier.id=Dossier_Avocat_Adversaire.Id_Dossier and Id_Avocat_Adversaire =", "advr")
            ElseIf Comb_choisi.Text = "محامي المكتب" Then
                Rech_Avo_Adver("select Ref from Dossier,Dossiers_Avocat_Bureau where Dossier.id=Dossiers_Avocat_Bureau.Id_Dossier and Id_Avocat_Bureau =", "clt")
            ElseIf Comb_choisi.Text = "" And Comb_ahkamIjra2.Text = "الأحكام و القرارات" Then
                Rech_ahkam("select id from Dossier where id in (select distinct Id_Dossier from Decision where [Type]='قطعي'and Date_Decision BETWEEN '" & Format(Date_min.Value.Date, "yyyy-MM-dd") & "' and '" & Format(Date_illa.Value.Date, "yyyy-MM-dd") & "')")
            ElseIf Comb_choisi.Text = "" And Comb_ahkamIjra2.Text = "الإجراءات" Then
                Rech_ahkam("select id from Dossier where id in (select distinct Id_Dossier from Ijraa where Date_Ijraa BETWEEN '" & Format(Date_min.Value.Date, "yyyy-MM-dd") & "' and '" & Format(Date_illa.Value.Date, "yyyy-MM-dd") & "')")
            ElseIf Comb_choisi.Text = "" And Comb_ahkamIjra2.Text = "الأحكام و القرارات و الإجراءات" Then
                Rech_ahkam("select distinct id from Dossier where id in (select distinct Id_Dossier from Decision where [Type]='قطعي'and Date_Decision BETWEEN '" & Format(Date_min.Value.Date, "yyyy-MM-dd") & "' and '" & Format(Date_illa.Value.Date, "yyyy-MM-dd") & "')or id in (select distinct Id_Dossier from Ijraa where Date_Ijraa BETWEEN '" & Format(Date_min.Value.Date, "yyyy-MM-dd") & "' and '" & Format(Date_illa.Value.Date, "yyyy-MM-dd") & "')")
            End If
            '-------------------------------remplissage---------------------------------------
            remplir()
            '---------------multi ligne in columns ;)-----------------------------------------
            For k As Integer = 0 To DataGridView1.Columns.Count - 1
                DataGridView1.Columns(k).DefaultCellStyle.WrapMode = DataGridViewTriState.True
            Next
            '----------------------trie datagridview----------------------------------------------------------
            Try
                Dim s As Integer = -1
                ' Dim y As Integer = -1
                For i As Integer = 0 To DataGridView1.Columns.Count - 1
                    If DataGridView1.Columns(i).Name = "تاريخ آخر جلسة" Then
                        s = i
                    End If
                    If DataGridView1.Columns(i).Name = "تاريخ الحكم أو القرار" Then
                        y = i
                    End If
                Next
                If s <> -1 Then
                    Dim Column2 As DataGridViewColumn = DataGridView1.Columns(s)
                    For Each r As DataGridViewRow In DataGridView1.Rows
                        If r.Cells(Column2.Index).Value = "" Then
                            r.Cells(Column2.Index).Value = Nothing
                        Else
                            r.Cells(Column2.Index).Value = Convert.ToDateTime(r.Cells(Column2.Index).Value)
                        End If


                    Next
                End If
                ' DataGridView1.Sort(DataGridView1.Columns(s), System.ComponentModel.ListSortDirection.Ascending)
                If y <> -1 Then
                    Dim Column3 As DataGridViewColumn = DataGridView1.Columns(y)
                    For Each r As DataGridViewRow In DataGridView1.Rows
                        If r.Cells(Column3.Index).Value = "" Then
                            r.Cells(Column3.Index).Value = Nothing
                        Else
                            r.Cells(Column3.Index).Value = Convert.ToDateTime(r.Cells(Column3.Index).Value)
                        End If
                    Next
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub Btn_parc_nom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_nom.Click
        If Comb_choisi.Text = "الموكل" Then
            Comb_nom.Name = "Comb_nom"
            Me.Enabled = False
            Partager.frmMoule = New Form
            Partager.frmMoule = Me
            Partager.type_rech_mowakil = "Frm_Ref"
            Frm_Rech_Clients.Show()
        ElseIf Comb_choisi.Text = "الخصم" Then
            Comb_nom.Name = "Comb_adv"
            Me.Enabled = False
            Partager.frmMoule = New Form
            Partager.frmMoule = Me
            Partager.type_rech_adver = "Frm_Idafa_khossoum"
            Frm_Rech_Adver.Show()
        ElseIf Comb_choisi.Text = "محامي الخصم" Then
            Me.Enabled = False
            Partager.rech = True
            Partager.type_rech_Avo_adver = Me.Name
            Frm_Rech_Avo_Adver.Show()
        ElseIf Comb_choisi.Text = "محامي المكتب" Then
            Me.Enabled = False
            Partager.rech = True
            Partager.type = Me.Name
            Frm_Rech_Avoc_Clients.Show()
        ElseIf Comb_choisi.Text = "الممثل القانوني للموكل" Then
            Me.Enabled = False
            Partager.frmMoule = New Form
            Partager.frmMoule = Me
            Partager.type = "الممثل القانوني للموكل"
            Frm_Representant.Show()
        ElseIf Comb_choisi.Text = "الممثل القانوني للخصم" Then
            Me.Enabled = False
            Partager.frmMoule = New Form
            Partager.frmMoule = Me
            Partager.type = "الممثل القانوني للخصم"
            Frm_Representant.Show()
        ElseIf Comb_choisi.Text = "" Then
            MsgBox("عفوا، عليك اختيار ما تريد البحث عليه", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Enabled = False
        Frm_Columns.Show()
    End Sub

    Private Sub Comb_ahkamIjra2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_ahkamIjra2.SelectedIndexChanged
        If Comb_ahkamIjra2.Text <> "" Then
            Date_min.Enabled = True
            Date_illa.Enabled = True
        Else
            Date_min.Enabled = False
            Date_illa.Enabled = False
        End If
    End Sub

    Private Sub Btn_print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_print.Click
        '-------------------paysage-----------------------
        PrintDocument1.DefaultPageSettings.Landscape = True
        '-------------------------------------------------
        y = 0
        m = 1
        PrintPreviewDialog1.ShowDialog()

    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim format As New StringFormat(StringFormatFlags.DirectionRightToLeft)
        Dim heigth As Integer = 100
        Dim width As Integer = 0
        Dim p As New Pen(Brushes.Black, 2.0F)
        Dim f As New System.Drawing.Font("times new roman", 9, FontStyle.Bold)
        Dim fe As New System.Drawing.Font("times new roman", 9)
        '--------------------------------------------------
        e.Graphics.DrawString("الصفحة :" & m, f, Brushes.Black, New Point(50, 770))

        For i As Integer = DataGridView1.Columns.Count - 1 To 0 Step -1
            e.Graphics.FillRectangle(Brushes.DarkGray, New Rectangle(heigth, 100, DataGridView1.Columns(0).Width, DataGridView1.ColumnHeadersHeight))
            e.Graphics.DrawRectangle(p, New Rectangle(heigth, 100, DataGridView1.Columns(0).Width, DataGridView1.ColumnHeadersHeight))
            e.Graphics.DrawString(DataGridView1.Columns(i).HeaderText.ToString, f, Brushes.Black, New Rectangle(heigth - 2, 100 + 2, DataGridView1.Columns(0).Width, DataGridView1.ColumnHeadersHeight + 2), format)

            heigth = heigth + DataGridView1.Columns(i).Width
        Next
        '--------------------------------------------------
        heigth = 100 + DataGridView1.ColumnHeadersHeight

        While y <= DataGridView1.Rows.Count - 1

            If heigth >= e.MarginBounds.Height + 100 Or heigth + DataGridView1.Rows(y).Height > e.MarginBounds.Height + 100 Then
                e.HasMorePages = True
                heigth = 100
                m += 1
                Return
            End If

            width = 100
            For j As Integer = DataGridView1.Columns.Count - 1 To 0 Step -1
                e.Graphics.DrawRectangle(p, New Rectangle(width, heigth, DataGridView1.Columns(0).Width, DataGridView1.Rows(y).Height + 20))
                e.Graphics.DrawString(DataGridView1.Rows(y).Cells(j).FormattedValue.ToString, fe, Brushes.Black, New Rectangle(width + 1, heigth + 1, DataGridView1.Columns(0).Width - 1, DataGridView1.Rows(y).Height + 20), format)
                width += DataGridView1.Columns(j).Width
            Next
            heigth += DataGridView1.Rows(y).Height + 20

            y += 1
            If y = DataGridView1.Rows.Count - 1 Then
                y = 0
                m = 1
                Exit While
            End If
        End While

    End Sub

End Class